<?php
/*

v1.02
bach
2017-05-22

query($request)订单查询
1 wx_orderQuery($request)

wx_microPay($request)刷卡支付 wx_scan
wx_jsApi($request)网页支付 wx_qrcode
wx_native($request)扫码支付 wx_app

wx_jsApiReceive($request)被动接收结果

admin($request)管理员查帐支付

account($request)账户支付

refund($request)退款

accountPay($request) 会员自己支付
 
*/

namespace app\common\model;
class Pay{
    private $request;
    private $fee_max = 20000;
    private $admin_user_id;
    private $user_id;
    private $user;
    var $config;
    function __initial(){
        ini_set('date.timezone','Asia/Shanghai');
    }
    function __construct(){
        $this->user = $user = new User;
        $this->user_id = $user->getUserId();
        $this->config = getConfig('pay');
        //json_decode('{"ADMIN":{"id":1,"neccessary":{}},"ACCOUNT":{"id":2,"neccessary":{}},"MICROPAY":{"id":101,"neccessary":{	"body":""}},"JSAPI":{"id":102,"neccessary":{	"body":""}},"NATIVE":{"id":102,"neccessary":{	"body":""}},"BARPAY":{"id":201,"neccessary":{"body":""}},"QRPAY":{"id":202,"neccessary":{"body":""}}}',1);
    }
    // private function order_test(){
    // 	// return;
    // 	$order_id = db('order')->insertGetId(
    // 		['create_time'=>time(),'state'=>1,'total_fee'=>10,'class'=>2,'user_id'=>33]
    // 		);
    // 	dump($order_id);
    // 	$this->request['order_id'] = $order_id;
    // 	return $order_id;
    // }
    //检查支付前检查request参数
    private function inputCheck($demand,$self = false){
        //检查order
        if(!isset($this->request['order_id'])){
            return array('state'=>0,'errormsg'=>'order_id missing');
        }
        if(!isset($this->request['order_no'])){
            return array('state'=>0,'errormsg'=>'order_no missing');
        }
        $order_id = (int)$this->request['order_id'];
        if(!$order_id || $order_id == 0){
            return array('state'=>0,'errormsg'=>'order_id missing');
        }
        $orderFind = db("pay")->where(['order_id'=>$order_id])->find();
        if($orderFind){
            return array('state'=>0,'errormsg'=>'order paid');
        }
        $order = new Order;
        $order = $order->getInfo($order_id);
        if( !isset($order['state']) || !$order['state'] ){
            return $order;
        }
        if(!$order['data']['info']['time'] || !$order['data']['info']['pay_in'] || time()>$order['data']['info']['pay_in']+$order['data']['info']['time']){
            return array('state'=>0,'errormsg'=>'order expired');
        }
        $order_class = $order['data']['info']['class'];
        if(!isset($this->config[$demand]) || !$this->config[$demand]){
            // Action::add('pay_channel_error',$order_id,$demand);
            return array('state'=>0,'errormsg'=>'channel error','channel'=>$demand);
        }
        if($demand == 'ACCOUNT' && $order_class == 'recharge' ){
            // Action::add->add(pay_channel_error,$order_id,"ACCOUNT recharge");
            return array('state'=>0,'errormsg'=>'channel not allowed');
        }
        if( !$self && in_array($demand, array('ADMIN','ACCOUNT','MICROPAY','UNION')) ){
            $admin = new Admin;
            $authority = $admin->is($order_class);
            if(isset($authority['chief']) && $authority['chief']){
                // D(Action)->add('pay_admin_error',$order_id,chief);
                return array('state'=>0,'errormsg'=>'chief authority incorrect','order_class'=>$order_class);
            }
            if(!isset($authority['state']) || !$authority['state']){
                // D(Action)->add('pay_admin_error',$order_id);
                return $authority;
            }
        }
        //检查账单价格
        $total_fee = $order['data']['info']['total_fee'];
        if($total_fee <=0 ){
            return array('state'=>0,'errormsg'=>'total_fee incorrect');
        }
        // if($total_fee > $this->fee_max*100){
        // return array('state'=>0,'errormsg'=>'fee cannot more than '.$this->fee_max.'RMB');
        // }
        //检查账单参数(通过配置文件)
        $config = $this->config;
        if(!$config){
            return array('state'=>0,'errormsg'=>'config incorrect');
        }
        // $demand = strtolower($demand);
        $config = $config[$demand];
        if(!$config){
            return array('state'=>0,'errormsg'=>'config demand incorrect');
        }
        foreach($config['neccessary'] as $key => $reg){
            if(!isset($this->request[$key])|| !$this->request[$key]){
                return array('state'=>0,'errormsg'=>"need $key");
            }
            if($reg && !preg_match($reg,$this->request[$key])){
                return array('state'=>0,'errormsg'=>"need correct $key");
            }
            $payData[$key] = $this->request[$key];
        }
        $payData['order_id'] = $order_id;
        $payData['total_fee'] = $total_fee;
        return array('state'=>1,'errormsg'=>'ok','payData'=>$payData);
    }
    //储存pay数据
    private function addIntoDatabase($addData){
        $need = array('order_id','total_fee','channel','order_no');
        //检查数据齐全
        foreach ($need as $key) {
            if(!isset($addData[$key]) || !$addData[$key]) return array('errormsg'=>"need $key");
        }
        //检查渠道正确
        $channel = $addData['channel'];
        if(!isset($this->config[$channel])){
            return ['state'=>0,'errormsg'=>'config channel error'];
        }
        $channel = $this->config[$channel]['id'];
        if(!$channel) return array('errormsg'=>"channel id error");
        $addData['channel'] = $channel;
        //检查是否已存在
        $order_id = $addData['order_id'];
        $exist = db('pay')->where(['order_id'=>$order_id])->find();
        if($exist) return array('errormsg'=>'order_id exist in pay');
        //检查微信支付下获取用户user号
        $addData['user_id'] = $user_id = isset($addData['user_id'])?$addData['user_id']:0;
        if(!$user_id && isset($addData['openid']) && $addData['openid']){
            $openid = $addData['openid'];
            $whoPaied = $this->user->exist(array('openid'=>$openid));
            if($whoPaied['state']){
                $user_id = $whoPaied['data']['user_id'];
            }else{
                $user_id = 0;
            }
            $addData['user_id'] = $user_id;
        }
        //时间
        $addData['create_time'] = isset($addData['time_end'])?strtotime($addData['time_end']):time();
        $pay_id = db('pay')->insertGetId($addData);
        if(!$pay_id) return array('errormsg'=>'database pay save error');
        // dump(M()->getLastSql());
        return array('state'=>1);
    }

    //--------------------------------------------查询订单支付情况-------------------------------------------
    function query($request){
        if(!isset($request['order_id'])){
            return ['state'=>0,'errormsg'=>'need order_id'];
        }
        $order_id = (int)$request['order_id'];
        if(!$order_id || $order_id == 0){
            return array('state'=>0,'errormsg'=>'order_id missing');
        }
        //数据库已储存
        $orderFind = db("pay")->field('total_fee')->where(['order_id'=>$order_id])->find();
        if($orderFind){
            return array('state'=>1,'errormsg'=>'order paid','data'=>array('total_fee'=>$orderFind['total_fee']));
        }
        //获取订单信息
        $order = new Order;
        $order = $order->getInfo($order_id);
        if(!isset($order['state']) || !$order['state'] ){
            return array('state'=>0,'errormsg'=>'order not exist','errocode'=>'pay19682231');
        }
        $order_class = $order['data']['info']['class'];
        $admin = new Admin;
        $admin = $admin->is($order_class);
        if(isset($admin['state']) && $admin['state'] && !isset($admin['chief'])){
            $isAdmin = true;
        }else{
            $isAdmin = false;
        }
        $tenpay = new Tenpay;
        $tenpay = $tenpay->refer($order_id);
        if(isset($tenpay['state']) && $tenpay['state']){
            if($isAdmin){
                $tenpay['data']['admin_user_id'] = $this->user_id;
            }
            $add = $tenpay['data'];
            $add['channel'] = $add['trade_type'];
            $save = $this->addIntoDatabase($add);
            return $tenpay;
        }
        $alipay = new Alipay;
        $alipay = $alipay->_query($order_id);
        if(isset($alipay['state']) && $alipay['state']){
            if(false && $isAdmin){
                $alipay['data']['admin_user_id'] = $this->user_id;
            }
            if(isset($alipay['data']['send_pay_date'])){
                $alipay['data']['time_end'] = $alipay['data']['send_pay_date'];
            }
            $add = $alipay['data'];
            unset($add['open_id']);
            $save = $this->addIntoDatabase($add);
            return $alipay;
        }
        return array('state'=>0,'errormsg'=>'order not paid','channel'=>'query');
    }
    //--------------------------------------------被动接收订单支付结果-------------------------------------------
    // function tenpay_receive(){
    // 	$result = D(Tenpay)->_receive();
    // 	$this->paySaveIntoDatabase($result);
    // }

    //--------------------------------------------刷卡支付启动函数--------------------------------------------
    function scan($request){
        $this->request = $request;
        if(!isset($request['auth_code'])){
            return ['state'=>0,'errormsg'=>'need auth_code'];
        }
        $auth_code = $request["auth_code"];
        switch(substr($auth_code,0,2)){
            case "13":
                $channel = new Tenpay;
                $method = 'MICROPAY';
                $action = 'micro';
                break;
            case "28":
                $channel = new Alipay;
                $method = 'BARPAY';
                $action = '_barpay';
                break;
            default:
                return array('errormsg'=>'auth_code error');
        }
        $res = $this->inputCheck($method);
        if( !$res['state'] ){
            return $res;
        }
        if(!isset($this->request["auth_code"]) || $this->request["auth_code"] == ""){
            return array('state'=>0,'errormsg'=>"auth_code missing");
        }
        $payData = $res['payData'];
        $payData['auth_code'] = $auth_code;
        $result = $channel->$action($payData);
        if($result['state']){
            $add = $result['data'];
            $add['admin_user_id'] = $this->user_id;
            $add['channel'] = $method;//$add[trade_type];
            $add['order_id'] = $request['order_id'];
            // 存数据库必须的值 order_id,total_fee,channel

            $res = $this->addIntoDatabase($add);
        }
        return $result;
    }
    function receive_tenpay(){
        $tenpay = new Tenpay;
        $result = $tenpay->passive();
        if(array_key_exists("return_code", $result)
            && array_key_exists("result_code", $result)
            && $result["return_code"] == "SUCCESS"
            && $result["result_code"] == "SUCCESS")
        {
            $result['channel'] = $result['trade_type'];//$add[trade_type];
            $result['order_no'] = $result['out_trade_no'];
            $result['order_id'] = substr($result['out_trade_no'],-3);
            // 存数据库必须的值 order_id,total_fee,channel
            $res = $this->addIntoDatabase($result);
            echo 'success';
        }
        return $result;
    }
    function receive_alipay(){
        $alipay = new Alipay;
        $result = $alipay->comeback();
        if ($result['state']&&$result['data']['trade_status'] == 'TRADE_SUCCESS') {
            $channel = json_decode($result['data']['fund_bill_list'], 1)[0]['fundChannel'];
            $data['channel'] = $channel;
            $data['order_id'] = $result['data']['out_trade_no'];
            $data['total_fee'] = $_POST['total_amount']*100;
            $res = $this->addIntoDatabase($data);
            echo 'success';
        }else{
            echo 'fail';
        }

        return $result;
    }

    //--------------------------------------------网页订单支付--------------------------------------------
    function tenpay_jsapi($request){
        $user_id = $this->user_id;
        if(!$user_id){
            return array('state'=>0,'errormsg'=>'请登录');
        }
        $openId = $this->user->getOpenId();
        if(!$openId){
            return array('state'=>0,'errormsg'=>'need openid');
        }
        $this->request = $request;
        //检查数据
        $method = "JSAPI";
        $res = $this->inputCheck($method);
        if( !$res['state'] ){
            return $res;
        }
        $payData = $res['payData'];
        $payData['openid'] = $openId;
        $tenpay = new Tenpay;
        $result = $tenpay->jsapi($payData);
        return $result;
    }
    function alipay_jsapi($request){
        $user_id = $this->user_id;
        if(!$user_id){
            return array('state'=>0,'errormsg'=>'请登录');
        }
        $openId = $this->user->getOpenId();
        if(!$openId){
            return array('state'=>0,'errormsg'=>'need openid');
        }
        $this->request = $request;
        //检查数据
        $method = "JSAPI";
        $res = $this->inputCheck($method);
        if( !$res['state'] ){
            return $res;
        }
        $payData = $res['payData'];
        $payData['price'] = $payData['total_fee']/100;
        $alipay = new Alipay;
        $result = $alipay->wap_pay($payData);
        return $result;
    }

    //--------------------------------------------网页扫码支付--------------------------------------------
    function tenpay_qrcode($request){
        $this->request = $request;
        //检查数据
        $method = "NATIVE";
        $res = $this->inputCheck($method);
        if( !$res['state'] ){
            return $res;
        }
        $payData = $res['payData'];
        $tenpay = new Tenpay;
        $result = $tenpay->native($payData);
        if($result['state']){
            $result['data']['total_fee'] = $payData['total_fee'];
        }
//		dump($result);die;
        return $result;
    }
    function alipay_qrcode($request){
        $this->request = $request;
        //检查数据
        $method = "QRPAY";
        $res = $this->inputCheck($method);
        if( !$res['state'] ){
            return $res;
        }
        $payData = $res['payData'];
        $alipay = new Alipay;
        $result = $alipay->_qrpay($payData);
        if($result['state']){
            $result['data']['total_fee'] = $payData['total_fee'];
        }
        return $result;
    }


    //----------------------------------------收银员现场支付---------------------------------------
    function admin($request){
        $method = "ADMIN";
        $this->request = $request;
        $res = $this->inputCheck($method);
        if(!isset($res['state']) || !$res['state']){
            return $res;
        }
        $order_id = $request['order_id'];
        $query = $this->query($request);
        if($query['state']){
            // $query['state'] = 0;
            return $query;
        }
        $total_fee = $res['payData']['total_fee'];
        $result = array(
            'order_id' 		=> $order_id,
            'user_id' 		=> 0,
            'admin_user_id'	=> $this->user_id,
            'total_fee'  	=> $total_fee,
            'channel' 		=> $method,
        );
        $res = $this->addIntoDatabase($result);
        if(!isset($res['state']) || !$res['state']){
            return $res;
        }
        return array('state'=>1);
    }
    //-----------------------------------------report----------------------------------------
    function alipay_report($data){
        $alipay = new Alipay;
        // $date = strtotime($data['start']);
        // $date = date('Y-m',$date);
        // dump($data);
        // die();
        $res = $alipay->download($data);
        return $res;
        // dump($res);
    }
    function tenpay_report($input){

    }
    //-----------------------------------------会员账户支付----------------------------------------
    function account($request){
        $method = "ACCOUNT";
        $this->request = $request;
        $res = $this->inputCheck($method);
        if(!isset($res['state']) || !$res['state']){
            return $res;
        }
        $order_id = $request['order_id'];
        $query = $this->query($request);
        if($query['state']){
            $query['state'] = 0;
            return $query;
        }
        $total_fee = $res['payData']['total_fee'];
        //验证账户身份
        //验证账户身份
        //验证账户身份
        $auth_code = $this->request["auth_code"];
        if(!isset($auth_code) || $auth_code == ""){
            return array('state'=>0,'errormsg'=>"auth_code missing");
        }
        if(is_numeric($auth_code)){

            //从卡片方法获得user_id
            $card = new Card;
            $res = $card->getUserIdByCode($auth_code);
            if($res['state']){
                $whoPaied_user_id = $res['data']['user_id'];
                $method_id = 1 ;//card
            }
        }else{
            //从微信token方法获得user_id
            $token = new Token;
            $res = $token->check('pay_account',array('code'=>$auth_code));
            if(!$res['state']){
                return $res;
            }
            $whoPaied_user_id = $res['data']['user_id'];
            $method_id = 2 ;//wx_card
        }
        if(!$whoPaied_user_id){
            // D(Action)->add('pay_admin_error',$order_id);
            return array('state'=>0,'errormsg'=>'account auth error','errorcode'=>'pay946801');
        }
        $account = new Account();
        $res = $account->getBalanceByUserId($whoPaied_user_id);
        if(!$res['state']){
            return $res;
        }
        $balance = $res['data']['balance'];
        if($balance < $total_fee){
            return array('state'=>0,'errormsg'=>'account balance not enough','data'=>array('balance'=>$balance));
        }
        //验证完毕
        //验证完毕
        //验证完毕
        $result = array(
            'order_id' 		=> $order_id,
            'user_id' 		=> $whoPaied_user_id,
            'admin_user_id'	=> $this->user_id,
            'total_fee'  	=> $total_fee,
            'channel' 		=> $method,
            'method' 		=> $method_id,
        );
        $res = $this->addIntoDatabase($result);
        if(!isset($res['state']) || !$res['state']){
            return $res;
        }
        return array('state'=>1);
    }
    function member($request){
        $method = "ACCOUNT";
        $this->request = $request;
        $res = $this->inputCheck($method,$self = true);
        if(!isset($res['state']) || !$res['state']){
            return $res;
        }
        $order_id = $request['order_id'];
        $query = $this->query($request);
        if($query['state']){
            $query['state'] = 0;
            return $query;
        }
        $total_fee = $res['payData']['total_fee'];
        //验证账户身份
        //验证账户身份
        //验证账户身份
        $whoPaied_user_id = $this->user_id;
        if(!$whoPaied_user_id){
            return array('state'=>0,'errormsg'=>'not user exist','errorcode'=>'pay946845641');
        }
        $account = new Account();
        $res = $account->getBalanceByUserId($whoPaied_user_id);
        if(!$res['state']){
            return $res;
        }
        $balance = $res['data']['balance'];
        if($balance < $total_fee){
            return array('state'=>0,'errormsg'=>'account balance not enough','data'=>array('balance'=>$balance));
        }
        //验证完毕
        //验证完毕
        //验证完毕
        $result = array(
            'order_id' 	=> $order_id,
            'user_id' 		=> $whoPaied_user_id,
            'total_fee'  	=> $total_fee,
            'channel' 		=> $method,
        );
        $res = $this->addIntoDatabase($result);
        if(!isset($res['state']) || !$res['state']){
            return $res;
        }
        return array('state'=>1);
    }
    //-----------------------------------------银联支付-------------------------------------------
    function unionpay($request){
        $method = "UNION";
        $this->request = $request;
        $res = $this->inputCheck($method);
        if(!isset($res['state']) || !$res['state']){
            return $res;
        }
        $order_id = $request['order_id'];
        $query = $this->query($request);
        if($query['state']){
            // $query['state'] = 0;
            return $query;
        }
        $total_fee = $res['payData']['total_fee'];
        $result = array(
            'order_id' 		=> $order_id,
            'user_id' 		=> 0,
            'admin_user_id'	=> $this->user_id,
            'total_fee'  	=> $total_fee,
            'channel' 		=> $method,
        );
        $res = $this->addIntoDatabase($result);
        if(!isset($res['state']) || !$res['state']){
            return $res;
        }
        return array('state'=>1);
    }
    //--------------------------------------------退款--------------------------------------------
    function refund($request){
        $this->query($request);
        //用户检查
        $user_id = $this->user_id;
        if(!$user_id){
            return array('state'=>0,'errormsg'=>'请登录ed');
        }
        //需求金额检查
        if(!isset($request['refund_fee'])) return ['state'=>0,'errormsg' => 'need refund_fee'];
        $refund_fee = (int)$request['refund_fee'];
        if(!($refund_fee>0)){
            return array('state'=>0,'errormsg'=>'need refund_fee(int)');
        }
        //订单检查
        if(!isset($request['order_id'])) return ['state'=>0,'errormsg' => 'need order_id'];
        $order_id = (int)$request['order_id'];
        if(!$order_id || $order_id == 0){
            return array('state'=>0,'errormsg'=>'order_id missing');
        }
        $order = new Order;
        $order = $order->getInfo($order_id);
        if(!$order['state']){
            return $order;
        }
        $order_class = $order['data']['info']['class'];
        $whoPaied_user_id = $order['data']['info']['user_id'];
        //权限检查
        $admin = new Admin;
        $admin = $admin->is($order_class);
        if(!isset($admin['state']) || !$admin['state']){
            return array('state'=>0,'errormsg'=>'authority incorrect','errorcode'=>'refund2395849');
        }
        if(isset($admin['chief'])){
            return array('state'=>0,'errormsg'=>'chief authority incorrect','errorcode'=>'refund1296943');
        }
        //付款信息检查
        $where = array('order_id'=>$order_id);
        $pay = db('pay')->where($where)->find();
        $pay_total_fee = (int)$pay['total_fee'];
        $pay_time = $pay['create_time'];
        $pay_channel = (int)$pay['channel'];
        if(!$pay){
            $query = $this->query($request);
            if(!$query['state']){
                return $query;
            }
            $pay_total_fee = (int)$query['data']['total_fee'];
            $pay_time=strtotime($query['data']['time_end']);
        }
        //检查时间是否过期
        $refund_in = $order['data']['info']['refund_in'];
        if(!$refund_in || !$pay_time || time()>($refund_in+$pay_time)){
            return array('state'=>0,'errormsg'=>'order refund time expired','refund_in'=>($refund_in/60)."min");
        }
        if(!$pay_total_fee){
            return array('state'=>0,'errormsg'=>'pay total_fee incorrect');
        }
        //已退款信息统计
        $refund = db('pay_refund')->where($where)->select();
        $refund_total_fee = 0;
        foreach ($refund as $key => $value) {
            $refund_total_fee += $value['refund_fee'];
        }
        $refund_available = $pay_total_fee - $refund_total_fee;
        if($refund_available<=0){
            return array('state'=>0,'errormsg'=>'order already refunded');
        }
        if($refund_fee > $refund_available){
            return array('state'=>0,'errormsg'=>'refund_fee not available','remain'=>$refund_available);
        }
        if($order_class == 'recharge'){
            $account = new Account;
            $balance = $account->getBalanceByUserId($whoPaied_user_id);
            if(!$balance['state']){
                return $balance;
            }
            $balance = $balance['data']['balance'];
            if($refund_fee > $balance){
                return array('state'=>0,'errormsg'=>'account balance not enough','remain'=>$balance);
            }
        }
        //确认无误 开始退款
        $this->admin_user_id = $user_id;
        switch ($pay_channel) {
            case 1:
                return $this->refund_admin($order_id,$refund_fee);
                break;
            case 2:
                return $this->refund_account($order_id,$refund_fee);
                break;
            case 3:
                return $this->refund_union($order_id,$refund_fee);
                break;
            default:
                $refundData = array(
                    'order_id' 		=> $order_id ,
                    'pay_total_fee' 	=> $pay_total_fee ,
                    'refund_fee' 		=> $refund_fee ,
                    'refund_total_fee' 	=> $refund_total_fee ,
                );
                switch((int)($pay_channel/100)){
                    case 1:
                        $tenpay = new Tenpay;
                        $result = $tenpay->rebate($refundData);
                        break;

                    case 2:
                        $alipay = new Alipay;
                        $result = $alipay->_refund($refundData);
                        break;

                    default:
                        return array('errormsg'=>'order channel error');
                }
                if(isset($result['refund'])){
                    $refund_fee = $result['refund'];
                    if($refund_fee>0){
                        $add = array(
                            'create_time'=>time(),
                            'refund_fee'=>$refund_fee,
                            'order_id'=>$order_id,
                            'admin_user_id'=>$this->user_id,
                        );
                        $res = db('pay_refund')->insertGetId($add);
                        if(!$res) return array('errormsg'=>'refund database save error');
                    }
                }
                return $result;
        }
        return array('state'=>0,'errormsg'=>'channel incorrect','errorcode'=>'refund2398594512');
    }
    private function refund_admin($order_id,$refund_fee){
        $refund = array(
            'create_time'	=> time(),
            'order_id' 	=> $order_id,
            'refund_fee' 	=> $refund_fee,
            'admin_user_id' => $this->admin_user_id,
        );
        $res = db('pay_refund')->insertGetId($refund);
        if(!$res){
            return array('state'=>0,'errormsg'=>'refund database save error','channel'=>'admin');
        }
        return array('state'=>1);
    }
    private function refund_account($order_id,$refund_fee){
        $refund = array(
            'create_time'	=> time(),
            'order_id' 	=> $order_id,
            'refund_fee' 	=> $refund_fee,
            'admin_user_id' => $this->admin_user_id,
        );
        $res = db('pay_refund')->insertGetId($refund);
        if(!$res){
            return array('state'=>0,'errormsg'=>'refund database save error','channel'=>'admin');
        }
        return array('state'=>1);
    }
    private function refund_union($order_id,$refund_fee){
        $refund = array(
            'create_time'	=> time(),
            'order_id' 	=> $order_id,
            'refund_fee' 	=> $refund_fee,
            'admin_user_id' => $this->admin_user_id,
        );
        $res = db('pay_refund')->insertGetId($refund);
        if(!$res){
            return array('state'=>0,'errormsg'=>'refund database save error','channel'=>'admin');
        }
        return array('state'=>1);
    }
}
